package com.manor.im.activity

import android.view.View
import com.manor.common.activity.AbsActivity
import com.manor.common.utils.*
import com.manor.im.R
import com.manor.im.dialog.IMSGSearchDialog
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListAdapter
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListLayout
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationManagerKit
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationProvider
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo
import kotlinx.android.synthetic.main.activity_h_msg.*

/**
 *
 * @Project TaoMi
 * @version 1.0
 */
class HMsgActivity : AbsActivity() {
    var adapter: ConversationListAdapter? = null

    override fun getLayoutId(): Int = R.layout.activity_h_msg

    override fun main() {
        super.main()
        tvSearch.setOnClickListener {
            IMSGSearchDialog.load(this)
        }
        adapter = ConversationListAdapter(mutableListOf())
        mConversationList.setAdapter(adapter)
    }

    private fun initDate() {
        ConversationManagerKit.getInstance().loadConversation(object : IUIKitCallBack {
            override fun onSuccess(data: Any?) {
                adapter?.setDataProvider(data as ConversationProvider)
            }

            override fun onError(module: String, errCode: Int, errMsg: String) {
                ToastUtil.show("加载消息失败")
            }
        })
        mConversationList.setOnItemClickListener(object : ConversationListLayout.OnItemClickListener {
            override fun onDelClick(view: View?, position: Int, messageInfo: ConversationInfo?) {
                messageInfo?.let {
                    mConversationList.deleteConversation(position, it)
                }
            }

            override fun onItemClick(view: View?, position: Int, messageInfo: ConversationInfo?) {
                val avatar = if (messageInfo?.iconUrlList?.isNotEmpty() == true) messageInfo?.iconUrlList[0].toString() else ""
                messageInfo?.run {
                    HChatRoomActivity.forward(this@HMsgActivity, this.id, this.title, avatar, V2TIMConversation.V2TIM_C2C)
                }
            }

        })

    }


    override fun onResume() {
        super.onResume()
        initDate()
    }


}